// member/pages/coupon/coupon.js
let myRequest = require('../../../common/script/myRequest.js');
let api = require('../../../common/script/api.js');
let utils = require('../../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isEnd: true,
    isWithout: true,
    page: 1, // 页码
    pageSize: 30,
    listData: [],
    status: 0
  },
  navClick(e) {
    let that = this;
    if (e.target.dataset.status == that.data.status) {
      return;
    }
    that.setData({
      status: e.target.dataset.status,
      page: 1,
      isEnd: true,
      isWithout: true,
      listData: []
    })
    that.getData()
  },
  getData() {
    const that = this;
    let data = {
      p: that.data.page,
      page_size: that.data.pageSize,
      send_type:2,
      type: parseInt(that.data.status)
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getGoodsCouponList, data).then(res => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
      if (res.data.status == 1) {
        if (res.data.result.length == 0) {
          that.setData({
            isWithout: false
          })
        } else {
          res.data.result.forEach(item => {
            item.use_start_time = utils.timestampToDateFormat(item.use_start_time * 1000);
            item.use_end_time = utils.timestampToDateFormat(item.use_end_time * 1000);
            item.money = item.money.split('.')[0];
            item.condition = item.condition.split('.')[0];
            item.select = false;
          })
          that.setData({
            isWithout: true,
            listData: res.data.result
          })
        }
      } else {
        that.setData({
          listData: [],
          isWithout: false
        })
      }
      // else if (res.data.status == -101) {
      //   login.loginByWeixin().then(res => {
      //     app.globalData.userInfo = res.data.result;
      //     app.globalData.token = res.data.result.token
      //     that.getData()
      //   })
      // }
    })
  },
  toUseCoupon(e) {
    const that = this;
    wx.navigateTo({
      url: `/member/pages/useCoupon/useCoupon?id=${e.target.dataset.id}&status=${that.data.status}`,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const that = this;
    that.getData()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    const that = this;
    wx.showNavigationBarLoading()
    that.setData({
      page: 1,
      isEnd: true,
      isWithout: true,
    })
    that.setData({
      listData: []
    })
    that.getData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const that = this;
    if (!that.data.isEnd) {
      return
    }
    let data = {
      p: that.data.page + 1,
      page_size: that.data.pageSize,
      send_type:2,
      type: parseInt(that.data.status)
    }
    wx.showLoading({
      title: '加载中...',
    })
    myRequest.EasyHttp.post(api.reqUrls.getGoodsCouponList, data).then(res => {
      wx.hideLoading();
      if (res.data.status == 1) {
        if (res.data.result.length == 0) {
          wx.showToast({
            title: '没有更多了',
            icon: 'none',
            duration: 1000
          })
          that.setData({
            isEnd: false
          })
        } else {
          res.data.result.forEach(item => {
            item.use_start_time = utils.timestampToDateFormat(item.use_start_time * 1000);
            item.use_end_time = utils.timestampToDateFormat(item.use_end_time * 1000);
            item.money = item.money.split('.')[0];
            item.condition = item.condition.split('.')[0];
            item.select = false;
          })
          that.setData({
            listData: [...that.data.listData, ...res.data.result],
            page: that.data.page + 1
          })
        }
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage() {

  // }
})